package com.mxpio.erp.asset.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.asset.entity.BorrowApplyLine;
import com.mxpio.erp.common.asset.service.BorrowApplyLineService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * @Author : 吴盼盼
 * @Datetime : 2022/9/15 10:16
 * @Desc :
 * @Modor :  Modifytime:
 * @modDesc :
 */
@Tag(name = "AssetBorrowApplyLineController", description = "资产借用归还行接口")
@RestController
@RequestMapping("/erp/asset/borrowapplyline/")
public class AssetBorrowApplyLineController {
	@Autowired
	private BorrowApplyLineService borrowApplyLineService;
	@GetMapping("list")
    @Operation(summary = "资产借用归还行列表", description = "获取资产借用归还行列表（无分页）", method = "GET")
    public Result<List<BorrowApplyLine>> list(@Parameter(name="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<BorrowApplyLine> applyList = borrowApplyLineService.list(BorrowApplyLine.class, criteria);
        return Result.OK(applyList);
    }
	
    @GetMapping("page")
    @Operation(summary = "资产借用归还行列表", description = "资产借用归还行列表（分页）", method = "GET")
    public Result<Page<BorrowApplyLine>> page(@Parameter(name="查询参数") Criteria criteria, @Parameter(name="每页条数")Integer pageSize, @Parameter(name="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<BorrowApplyLine> warehouseOrders = borrowApplyLineService.listPage(BorrowApplyLine.class, page, criteria);
        return Result.OK(warehouseOrders);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据applyNo资产借用归还行", description = "根据applyNo资产借用归还行", method = "GET")
    public Result<List<BorrowApplyLine>> getByParentId(@Parameter(name="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<BorrowApplyLine> applyLineList = borrowApplyLineService.list(BorrowApplyLine.class, c);
        if (applyLineList.size()>0){
            borrowApplyLineService.handleBorrowApplyLinesReturnedQuantity(applyLineList);//关联已归还数量
        }
        return Result.OK(applyLineList);
    }

}
